package server.handler;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

import qq.message.DataPackage;
import qq.message.DataPackage.MessageType;
import server.child.thread.ServerChildThread;
import server.child.thread.handler.ChildThreadHandle;

public class ServerHandler {
	public static Map<String,ServerChildThread> userMap=new HashMap<String,ServerChildThread>();
	public void server() throws IOException, ClassNotFoundException
	{
		ServerSocket ss=new ServerSocket(8888);
		Socket socket;
		while(true)
		{
			if((socket=ss.accept())!=null)
			{
				System.out.println("get client ");
				OutputStream os=socket.getOutputStream();
				InputStream is=socket.getInputStream();
				ObjectOutputStream oos=new ObjectOutputStream(os);
				ObjectInputStream ois= new ObjectInputStream(is);
				DataPackage data;
				data=(DataPackage)ois.readObject();
					System.out.println("get client data ");
					if((data.getMessageType())==MessageType.LOGIN)
					{
						if(userMap.containsKey(data.getFromName()))
						{
							System.out.println("CHONGFU LOGIN ");
						}
						else
						{
							ServerChildThread sct=new ServerChildThread(data.getFromName(),socket,os,is,ois,oos);
//							System.out.println("add client "+data.getFromName());
							userMap.put(data.getFromName().trim(), sct);
							sct.getOos().writeObject(this.loginSucceed(data));
							System.out.println("reply to  client start operation "+data.getFromName());
							ChildThreadHandle.handleLogin(data);
							System.out.println("put client to userMap :"+data.getFromName());
//							System.out.println(userMap);
							Thread td=new Thread(sct);
							td.start();
//							System.out.println("client "+data.getFromName()+" start");
						    System.out.println(this.loginSucceed(data).getFromName());
						    System.out.println(this.loginSucceed(data).getMessageData());
						    System.out.println(this.loginSucceed(data).getMessageType());
						}
						
					}
					else
					{
						oos.writeObject(this.loginFailed(data));
					}
					
				}
		}
	}
	
	/**
	 * 返回失败类型的包
	 * @param data server接收的来自某一客户端的第一个包
	 * @return
	 */
	public DataPackage loginFailed(DataPackage data){
		DataPackage temp=new DataPackage();
		temp.setFromName("server");
		temp.setToName(data.getFromName());
		temp.setMessageData("nak");
		temp.setMessageType(MessageType.LOGOUT);
		return temp;
		
	}
	
	/**
	 * 返回登陆成功类型的包
	 * @param data server接收的来自某一客户端的第一个包
	 * @return
	 */
	public DataPackage loginSucceed(DataPackage data){
		DataPackage temp=new DataPackage();
		temp.setFromName("server");
		temp.setToName(data.getFromName());
		temp.setMessageData("ack");
		temp.setMessageType(MessageType.LOGIN);
		temp.setTime(System.currentTimeMillis());
		return temp;
		
	}
}
